﻿using System;
using System.Web.Mvc;
using System.Data;
using SN.Data;
using Agro.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Ninject;
using Agro.Services;
using Agro.Library;
using SN.Core;


namespace Agro.Web.Areas.SystemManage.Controllers
{
    public class MenuController : BaseController
    {
        //
        // GET: /SystemManage/Menu/ 
        [Inject]
        public IMenuService MenuService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetMenuTreeNode(string pid)
        {
            DataTable dataTable = MenuService.GetMenuDT(pid);
            DataColumn dc = new DataColumn("isParent", typeof(bool));
            if (dataTable.Rows.Count == 0 && string.IsNullOrEmpty(pid))
            {
                DataRow dr = dataTable.NewRow();
                dr["menu_id"] = "0";
                dr["menu_name"] = "菜单管理";
                dr["menu_parentid"] = "-1";
                dataTable.Rows.InsertAt(dr, 0);
                dc.DefaultValue = true;
                dataTable.Columns.Add(dc);
            }
            else
            {
                dc.Expression = "IIF(MENU_ISDETAIL=1, true, false)";
                dataTable.Columns.Add(dc);
            }
            string result = JsonConvert.SerializeObject(dataTable, new DataTableConverter());
            return Content(result);
        }

        public ActionResult Edit_Menu(string id, string parentid)
        {
            T_Menu menu = new T_Menu();
            if (string.IsNullOrEmpty(id))
            {
                menu.Menu_ParentID = int.Parse(parentid);
                menu.Menu_IsShow = "1";
                menu.Menu_Order = 1;
            }
            else
            {
                menu = MenuService.GetEntity<T_Menu>(T_Menu._.Menu_ID == id);
            }
            //return View("EditMenu", menu);
            return View(menu);
        }

        [HttpPost]
        public ActionResult Save_Menu(string Menu_id, FormCollection collection)
        {
            T_Menu Menu = new T_Menu();
            if (!string.IsNullOrEmpty(Menu_id.Trim()) && Convert.ToInt32(Menu_id)>0)
            {
                Menu = MenuService.GetEntity<T_Menu>(T_Menu._.Menu_ID ==Convert.ToInt32(Menu_id));
            }
            else 
            {
                Menu.Menu_IsDel = "0";
                Menu.Menu_IsDetail = "0";
                Menu.Menu_CreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                Menu.Menu_CreateUserID = CurrUser.UserInfo.User_Name;
                Menu.Menu_IsPermission = "0";
                //Menu.Menu_ShowPosition = "0";
                //Menu.Menu_LayerIndex = 0;
            }
            UpdateModel(Menu, collection);
            string msg = string.Empty;
            MenuService.SaveMenu(Menu, out msg);
            return SNJson(Menu.Menu_ID, msg);
        }

        public ActionResult Delete_Menu(string id, string pid, int childNum)
        {
            string returnValue = "0";
            returnValue = MenuService.DeleteMenu(id, pid, childNum).ToString();
            return Json(returnValue);
        }

        public ActionResult GetMenuList(string menuid, QueryBase query)
        {
            if (string.IsNullOrEmpty(menuid)) return Json("");
            WhereClip wclip = null;
            if (!string.IsNullOrEmpty(menuid)) wclip = WhereClip.Bracket(T_Menu._.Menu_ParentID == menuid || T_Menu._.Menu_ID == menuid);
            var list = MenuService.GetMenuList(query, wclip);
            return JqGrid(list, query);
        }
    }
}
